
/**
 *  遮罩层效果
 */
(function(window, undefined) {
	var _thisScript, _path;
	
	var maskLayer = function (param){
		var config = {};
		if (typeof param === 'string') {
			if(param=="true"){
				config.lock = true;
			} else if(param=="false"){
				config.lock = false;
			} else {
				//config = maskLayer.defaults;
			}
		} else if(typeof param === 'boolean') {
			config.lock = param;
		} else if (typeof param === 'object') {
			config = param;
		} else {
			//config = maskLayer.defaults;
		}
		// 合并默认配置
		var defaults = maskLayer.defaults;			
		for (var i in defaults) {
			if (config[i] === undefined ) {
				config[i] = defaults[i];
			}
		}
		if(maskLayer.list[config.id]){
			return maskLayer.list[config.id]._init(config);
		} else{
			return maskLayer.list[config.id] = new maskLayer.fn._init(config);
		}
	};
	
	maskLayer.fn = maskLayer.prototype = {
		_init: function(config){
			var that = this, DOM;
			
			that.DOM = DOM = that.DOM || that._getDOM();//maskLayer.DOM = {};//主要元素dom
			//lockMaskWrap的z-index
			if(!config.zIndex){
				config.zIndex = that._maxIndex();
			}
			if(config.follow && config.follow.nodeType === 1){
				config.zIndex = config.follow.style.zIndex -1;
			} 
			DOM.lockMaskWrap.style.zIndex = config.zIndex;
			//icon
			if(config.icon) {
				DOM.icon.style.height = "auto";
				DOM.icon.src = maskLayer.path + '/icons/' + config.icon + '.gif';
			} else { 
				DOM.icon.style.height = "0px";
			}
			//content
			if(config.content){
				if (typeof config.content === 'string') {
					DOM.content.innerHTML = config.content;
				} else if (config.content.nodeType === 1) {
					// 让传入的元素在对话框关闭后可以返回到原来的地方
					that._elemBack && that._elemBack();
					var display = config.content.style.display,
						prev = config.content.previousSibling,
						next = config.content.nextSibling,
						parent = config.content.parentNode;
					that._elemBack = function () { 
						if (prev && prev.parentNode) {
							prev.parentNode.insertBefore(config.content, prev.nextSibling);
						} else if (next && next.parentNode) {
							next.parentNode.insertBefore(config.content, next);
						} else if (parent) {
							parent.appendChild(config.content);
						};
						config.content.style.display = display;
						that._elemBack = null;
					};//that对象的又一属性	
					DOM.content.innerHTML='';
					DOM.content.appendChild(config.content);
					config.content.style.display = 'block';
				}
			} 
			//lockMask的opacity和backgroundColor
			DOM.lockMask.style.opacity = config.opacity;
			DOM.lockMask.style.filter = 'alpha(opacity=' + config.opacity * 100 + ')';
			DOM.lockMask.style.backgroundColor = config.bgcolor;
			//wrap
			if(config.lock){
				DOM.wrap.style.display = "block";
			} else {
				DOM.wrap.style.display = "none";
			}
			that.config = config;//maskLayer.config = {};//设置的配置
			
			return that;
		},
		
		/** 设置屏锁 */
		lock : function(config) {
			var that = this, DOM = that.DOM;
			
			if(config){
				//lockMaskWrap
				if(typeof(config.zIndex) != "undefined" && config.zIndex != that.config.zIndex){
					that.config.zIndex = config.zIndex;
					if(!config.zIndex){
						DOM.lockMaskWrap.style.zIndex = that._maxIndex();
					} else {
						DOM.lockMaskWrap.style.zIndex = config.zIndex;
					}
				}
				if(typeof(config.follow) != "undefined" && config.follow.nodeType === 1 && config.follow != that.config.follow){
					that.config.follow = config.follow;
					DOM.lockMaskWrap.style.zIndex = config.follow.style.zIndex -1;
				}
				//icon
				if(typeof(config.icon) != "undefined" && config.icon != that.config.icon){
					that.config.icon = config.icon;
					if(config.icon) {
						DOM.icon.style.height = "auto";
						DOM.icon.src = maskLayer.path + '/icons/' + config.icon + '.gif';
					} else {
						DOM.icon.style.height = "0px";
					}
				}
				//content
				if(typeof(config.content) != "undefined" && config.content != that.config.content){
					that.config.content = config.content;
					if(config.content){
						if (typeof config.content === 'string') {
							DOM.content.innerHTML = config.content;
						} else if (config.content.nodeType === 1) {
							// 让传入的元素在对话框关闭后可以返回到原来的地方
							that._elemBack && that._elemBack();
							var display = config.content.style.display,
								prev = config.content.previousSibling,
								next = config.content.nextSibling,
								parent = config.content.parentNode;
							that._elemBack = function () {
								if (prev && prev.parentNode) {
									prev.parentNode.insertBefore(config.content, prev.nextSibling);
								} else if (next && next.parentNode) {
									next.parentNode.insertBefore(config.content, next);
								} else if (parent) {
									parent.appendChild(config.content);
								}
								config.content.style.display = display;
								that._elemBack = null;
							};
							DOM.content.innerHTML='';
							DOM.content.appendChild(config.content);
							config.content.style.display = 'block';
						}
					} else {
						DOM.content.innerHTML='';
					}
				}
				//lockMask
				if(typeof(config.opacity) != "undefined" && config.opacity != that.config.opacity){
					that.config.opacity = config.opacity;
					DOM.lockMask.style.opacity = config.opacity || 0;
					DOM.lockMask.style.filter = 'alpha(opacity=' + (config.opacity || 0) * 100 + ')';
				}
				if(typeof(config.bgcolor) != "undefined" && config.bgcolor != that.config.bgcolor){
					that.config.bgcolor = config.bgcolor;
					DOM.lockMask.style.backgroundColor = config.bgcolor || "#fff";
				}
			}
			DOM.wrap.style.display = "block";
			return that;
		},

		/** 解开屏锁 */
		unlock : function(times) {
			var that = this;
			
			that._elemBack && that._elemBack();
			setTimeout(function(){that.DOM.wrap.style.display = "none";}, 1000 * (times || that.config.delay));
			return that;
		},
		
		_maxIndex : function(){
			/*return Math.max.apply(
				null,
				$.map($('body > *'),
				function(e,n){
					if($(e).css('position')=='absolute') {
						return parseInt($(e).css('z-index')) || 1;
					}
				})
			);*/
			var divs =document.getElementsByTagName("div"), max=0;
			for(var i=0; i<divs.length; i++){
				max =Math.max(max,divs[i].style.zIndex || 0);
			}
			return max;
		},
		
		_getDOM: function () {	
			var wrap = document.createElement('div'), DOM = {};
			wrap.innerHTML = maskLayer._templates;
			document.body.appendChild(wrap);
			DOM["wrap"] = wrap;
			
			DOM["lockMaskWrap"] = wrap.firstChild;
			DOM["icon"] = DOM["lockMaskWrap"].firstChild.firstChild.firstChild.firstChild.firstChild;
			DOM["content"] = DOM["icon"].nextSibling;
			DOM["lockMask"] = DOM["lockMaskWrap"].lastChild;
			
			return DOM;
		}
	};
	maskLayer.fn._init.prototype = maskLayer.fn;
	
	// 获取script路径
	_path = (function (script, i, me) {
		for (i in script) {
			// 如果通过第三方脚本加载器加载本文件，请保证文件名含有"masklayer"字符
			if (script[i].src && script[i].src.indexOf('masklayer') !== -1) {
				me = script[i];
			}
		};
		
		_thisScript = me || script[script.length - 1];
		me = _thisScript.src.replace(/\\/g, '/');
		return me.lastIndexOf('/') < 0 ? '.' : me.substring(0, me.lastIndexOf('/'));
	}(document.getElementsByTagName('script')));
	//var _lock = _thisScript.src.split('lock=')[1];
	
	maskLayer.path = _path;//资源路径
	maskLayer.defaults = {
		id: "id",//唯一标示
		zIndex: 9999,//null或undefined时为最上一层
		position: null,
		follow: null,//在这个元素之下
		content: null,//显示的内容，可以是string或dom对象
		icon: "loading2",//null或undefined时无图片
		bgcolor: "#fff",//遮罩层背景颜色，默认#fff
		opacity: 0,//遮罩层透明度，默认0.2
		delay: 0.5,//延时关闭遮罩层时间，单位是秒，默认0.5秒
		lock: false//初始化开启遮罩层，默认false
	};//默认的配置
	maskLayer._templates = ['<div style="width:100%;height:100%;position:fixed;top:0;left:0;z-index:1991;overflow:hidden;">',
	                        	'<table style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:2;" border="0" cellpadding="0" cellspacing="0"><tr align="center" valign="middle"><td><img style="visibility:hidden" onload=\'this.style.visibility="visible"\'/><div></div></td></tr></table>',
	                        	//'<iframe style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:0;filter:alpha(opacity=0);opacity:0;" src="about:blank" frameborder="0"></iframe>',
	                        	'<div style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:1;background-color:rgb(0, 0, 0);filter:alpha(opacity=0);opacity:0;"><iframe style="width:100%;height:100%;filter:alpha(opacity=0);opacity:0;" src="about:blank" frameborder="0"></iframe></div>',
	                        '</div>'].join('');
	maskLayer.list = {};//管理多个遮罩层
	
	window.maskLayer = maskLayer;
})(this);



/*
(function($, undefined) {
	
})(this.jQuery);*/

/*$(document).ready(function(){
	window.lock(true);
});
$(window).load(function(){
	window.lock(false);
});*/

